<%@page import="com.hexacta.hrs.domain.staffing.RequirementSkills"%>
<%@page import="com.hexacta.hrs.domain.staffing.Requirement"%>
<%@page import="com.hexacta.hrs.domain.staffing.Project"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/hexacta.tld" prefix="hexacta"%>

<%@ page import="org.vectrics.hibernate.ThreadProperties"%>
<%@ page import="org.vectrics.domain.Domain"%>
<%@ page import="java.util.Collection"%>
<%@ page import="java.util.List"%>
<%@ page import="org.vectrics.recruiter.RecruitServiceLocator"%>


<%@page import="org.vectrics.cv.Skill"%>
<%@page import="org.vectrics.cv.SkillType"%>
<%@page import="org.vectrics.cv.Level"%>
<%@page import="org.vectrics.provider.CoreServiceLocator"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.apache.commons.collections.Predicate"%>
<%@page import="org.apache.commons.collections.CollectionUtils"%>
<%@page import="org.vectrics.cv.PersonSkill"%>
<%@page import="org.vectrics.user.Person"%>
<%@page import="org.vectrics.cv.ExtraCertificate"%>
<%@page import="java.util.ArrayList"%>

<%@page import="org.vectrics.recruiter.struts.tag.cv.SkillsViewTag"%>
<%@page import="org.vectrics.cv.ListableSkill"%>
<%@page import="java.util.SortedMap"%>
<%@page import="java.util.SortedSet"%>
<%@page import="java.util.TreeSet"%>
<%@page import="java.util.Set"%><script language="JavaScript"
	type="text/javascript"
	src="/hrs-intranet/js/dynamicTree/DynamicTree.js"></script>
<script language="JavaScript" type="text/javascript"
	src="/hrs-intranet/js/dynamicTree/ie5.js"></script>
<link rel="stylesheet" type="text/css"
	href="/hrs-intranet/js/dynamicTree/DynamicTree.css" />


<%
    String contextPath = request.getContextPath();
	String reqId = request.getParameter("requirementId");
	String projectId = request.getParameter("selectedProjectId");
	
	Collection<Skill> parentSkillsAux = CoreServiceLocator.getPersonService().findAllParentSkill();
    Set<Skill> parentSkills = new TreeSet<Skill>(parentSkillsAux);

    Collection<Level> levels = CoreServiceLocator.getPersonService().findAllLevels();

    Project reqProject = CoreServiceLocator.getProjectServiceInterface().searchById(Long.parseLong(projectId));
	Requirement requirement = reqProject.findRequirement(Long.parseLong(reqId));
	Collection<RequirementSkills> requirementSkills = requirement.getRequirementSkills();
    Skill parent = null;

  	
/*     if (personSkills == null) {
        personSkills = new ArrayList<PersonSkill>();
    } 
*/
    for (Skill s : parentSkillsAux) {
        if (s.getName().equals("Certificaciones de Microsoft")) {
            parent = s;
            break;
        }
    }
    
    request.setAttribute("parentSkills", parentSkills);
    request.setAttribute("levels", levels);

%>

<head>
<sx:head cache="true" />
<link rel="stylesheet" href="/hrs-intranet/styles/displaytag.css"
	type="text/css" />
</head>
<s:include value="errorsSection.jsp"></s:include>
<s:hidden name="preRequirements" id="preRequirements" />
<s:form cssClass="formClass">
	<s:hidden name="requirementId" id="requirementId" />
	<s:hidden name="requirementProjectId" id="requirementProjectId" />
	<s:textfield name="requirementProjectName" label="Project"
		disabled="true" cssClass="dojoComboBox" />
	<sx:autocompleter id="roles" list="roles" listValue="description"
		searchType="startword" listKey="code" showDownArrow="false"
		keyName="requirementRoleCode" name="requirementRoleName"
		label="Assigned Role" />
	<sx:datetimepicker id="fromDate" name="requirementFromDate"
		label="Start Date" displayFormat="dd/MM/yyyy" />
	<sx:datetimepicker id="toDate" name="requirementToDate"
		label="End Date" displayFormat="dd/MM/yyyy" />
	<s:textfield label="Percentage (% FTE)" name="requirementPercentage"
		cssClass="dojoComboBox" />

	<table >
		<tr align="left">
			<th colspan="2"><label class="label">Skills:</label>
			</th>
		</tr>
		<tr>
			<td valign="top"><jsp:useBean id="skillForm"
					class="org.vectrics.recruiter.struts.cv.SkillForm" scope="request" />
				<table width="500">
					<tr>
						<td valign="middle">
							<div class="DynamicTree">
								<div class="wrap" id="tree">

									<%
									    // recorremos los skills padres - SKP1
									        // Los skill se cargan solo en 3 niveles como maximo.
									        for (Skill parentSkill : parentSkills) {

									            if ((parentSkill.getChildSkills() != null) && !parentSkill.getChildSkills().isEmpty()) {
									%>
									<div class="folder">
										<input type="checkbox" name="padre" value=""
											disabled="disabled" /><%=parentSkill.getName()%>

										<%
										    // recorremos los skills hijos - SKC1

										                Collection<Skill> skillsSubCategoriaAux = parentSkill.getChildSkills();
										                Set<Skill> skillsSubCategoria = new TreeSet<Skill>(skillsSubCategoriaAux);
										                for (Skill skill : skillsSubCategoria) {
										                    // si no tiene hijos lo dibujo
										                    if ((skill.getChildSkills() == null) || skill.getChildSkills().isEmpty()) {
										 							final Long idSkill = skill.getId();
										                        Predicate preSkill = new Predicate() {
										                            public boolean evaluate(Object objeto) {
										                                RequirementSkills skillPre = (RequirementSkills) objeto;
										                                return (skillPre.getSkill().getId().equals(idSkill));
										                            }
										                        };

										                        RequirementSkills skillEncontrado = (RequirementSkills) CollectionUtils
										                                .find(requirementSkills, preSkill);
										%>
										<%
										    if (skillEncontrado != null) {
										                            if (skill.getType() == SkillType.LEVELED_TYPE) {
										%>
										<div class="doc">
											<input type="checkbox"
												name="skill_<%=skillEncontrado.getSkill().getId()%>"
												value="<%=skill.getId()%>" checked="checked" /><%=skill.getName()%>
											<input type="hidden" id="l_<%=skill.getId()%>"
												value="<%=skillEncontrado.getLevel().getId()%>" />
										</div>
										<%
										    } else {
										%>

										<div class="doc boolean">
											<input type="checkbox"
												name="skill_<%=skillEncontrado.getSkill().getId()%>"
												value="<%=skill.getId()%>" checked="checked" /><%=skill.getName()%>
										</div>
										<%
										    }
										                        } else {  
										                        	if (skill.getType() == SkillType.LEVELED_TYPE) {
										%>
										<div class="doc">
											<input type="checkbox" name="skill_<%=skill.getId()%>"
												value="<%=skill.getId()%>" /><%=skill.getName()%>
											<input type="hidden" id="l_<%=skill.getId()%>" value="0" />
										</div>

										<%
										    } else {
										%>
										<div class="doc boolean">
											<input type="checkbox" name="skill_<%=skill.getId()%>"
												value="<%=skill.getId()%>" /><%=skill.getName()%>
										</div>
										<%
										    }
										                        }
										                        } else {
										                        //Si tiene hijos lo muestro y muestro sus hijos
										%>
										<div class="folder">
											<input type="checkbox" name="padre" value=""
												disabled="disabled" /><%=skill.getName()%>
											<%

											                        Collection<Skill> skillsAux = skill.getChildSkills();
											                        Set<Skill> skills = new TreeSet<Skill>(skillsAux);
											                        for (Skill child : skills) {
											                            final Long idSkill = child.getId();
										                        Predicate preSkill = new Predicate() {
										                            public boolean evaluate(Object objeto) {
										                                RequirementSkills skillPre = (RequirementSkills) objeto;
										                                return (skillPre.getSkill().getId().equals(idSkill));
										                            }
										                        };

										                        RequirementSkills skillEncontrado = (RequirementSkills) CollectionUtils
										                                .find(requirementSkills, preSkill);
											%>
											<div class="doc">
												<%
												    if (skillEncontrado != null) {
												%>
												<input type="checkbox"
													name="skill_<%=skillEncontrado.getSkill().getId()%>"
													value="<%=child.getId()%>" checked="checked" /><%=child.getName()%>
												<input type="hidden" id="l_<%=child.getId()%>"
													value="<%=skillEncontrado.getLevel().getId()%>" />
												<%
												    } else {
												%>
												<input type="checkbox" name="skill_<%=child.getId()%>"
													value="<%=child.getId()%>" /><%=child.getName()%>
												<input type="hidden" id="l_<%=child.getId()%>" value="0" />
												<%
												    }
												%>
											</div>
											<%
											    }
											%>
										</div>
										<%
										    }
										                } // END SKC1
										%>
									</div>
									<%
									    } // END IF
									        } // END SKP1
									%>
								</div>
							</div></td>
					</tr>
				</table>
				<div style="display: none;" id="divLevel">
					<select name="level" id="level">
						<%
					    // recorremos y armamos los options del combo level
					    for (Level level : levels) {
					%>
						<option value="<%=level.getId()%>"
						<% if (level.getName().equalsIgnoreCase("Intermediate")){ %>
							selected="selected"
							<% }%>
						><%=level.getName()%></option>
						<%
					}
					%>
					</select>
				</div> <script type="text/javascript">
					var tree = new DynamicTree("tree");
					tree.init();
				</script>
			</td>
		</tr>
		</table>
	<s:submit key="Cancel" cssClass="btn" id="addbtn"
		action="RequirementAction_filterRequirement" />
	<s:submit key="Ok" cssClass="btn" id="addbtn"
		action="RequirementAction_confirmEditRequirement"
		cssStyle="position:relative; left:-100px; top:-24px;" />
		
</s:form>